<template>
	<view class="box"
		style="padding: 0 1rem; height:98vh;box-sizing: border-box;display: flex;flex-direction: column;justify-content: space-between;">
		<u--form labelPosition="left" :model="model1" ref="uForm" labelWidth='120px'>
			<u-form-item label="姓名:" prop="createBy" placeholder="请输入姓名" borderBottom ref="item1">
				<u--input v-model="model1.createBy" border="none"></u--input>
			</u-form-item>
			<u-form-item label="当前身份:" prop="identityLable" placeholder="请输入姓名" borderBottom ref="item1">
				<div>{{ model1.identityLable }}</div>
			</u-form-item>
			<u-form-item label="下一阶段:" prop="toIdentityLable" placeholder="请输入姓名" borderBottom ref="item1">
				<div>{{ model1.toIdentityLable }}</div>
			</u-form-item>
			<u-form-item label="上传:" prop="name" ref="item1">
				<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
					></u-upload>
			</u-form-item>
			<u-form-item label="备注:" prop="remark" ref="item1">
				<u--textarea v-model="model1.remark" placeholder="请输入备注"></u--textarea>

			</u-form-item>
		</u--form>
		<u-button type="primary" style="margin-bottom: 50px;" @click="tijiao">提 交</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import { baseUrl } from '../../utils/config.js';

export default {
	data() {
		return {
			model1: {
				name: "",
				value1: "",
				toIdentityLable: "",
				identityLable: "",
			},
			fileName:"",
			fileList1: [],
			action: baseUrl + "/common/uploadwe"
		};
	},
	onLoad() {
		// console.log()
		this.model1.identityLable = uni.getStorageSync('identityLable');
		this.model1.toIdentityLable = uni.getStorageSync('toIdentityLable');
	},
	methods: {
		uploadSuccess(e) {
			console.log(e)
		},
		tijiao() {
			if(!this.model1.createBy){
				this.$refs.uToast.show({
					type: 'default',
					message: "请输入姓名",
				})
				return
			}
			console.log(this.fileName)
			if(!this.fileName){
				this.$refs.uToast.show({
					type: 'default',
					message: "请上传文件",
				})
				return
			}
		
			let that=this
			let data = {
				fileUrl: this.fileName,
				currentIdentity: uni.getStorageSync('toIdentityLable'),
				createBy: this.model1.createBy,
				remark: this.model1.remark
			}
			uni.request({
				url: `${baseUrl}/wechat/InfoIdentityUp`,
				method: 'post',
				data: data,
				header: { taken: uni.getStorageSync('ykt_openId') },
				success(res) {
					console.log(res)
					if(res.data.code==500){
						that.$refs.uToast.show({
							type: 'default',
							message: res.data.msg,
						})
						return
					}
					uni.switchTab({
						url: '/pages/index/index'
					});
					that.$refs.uToast.show({
						type: 'default',
						message: "提交成功",
					})
				
				},

			});
		},// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result,
						a:'sadsad'
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that=this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseUrl+'/common/uploadwe', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res.data)
							that.fileName=JSON.parse(res.data).fileName
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},

	},

};
</script>

<style></style>